<template>
		<div>
			<div>
				<!--<ul class="aui-list aui-media-list address_list"  style="text-align: left;">
					
				</ul>-->
				<ul class="aui-list aui-media-list address_list"  style="text-align: left;" >
					<li class="aui-list-item address-root" v-if="!login">  <!--v-if="!login"-->
						<div class="noLogin">
							
							<div class="showT">登录后使用常用地址</div>
							<div class="showLogin" @click="toLogin">
								<span>登录</span>
							</div>
							<!--<div style="clear: both;"></div>-->
						</div>
					</li>
					<li class="aui-list-item address-root" v-if="login && getAddressListHome && getAddressListHome.length == 0">  <!--v-if="!login"-->
						<div class="noLogin">
							
							<div class="showT">暂无收货地址</div>
		
						</div>
					</li>
					<!--<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-title">优惠金额</div>
							<div id="co_totalMinis" class="aui-list-item-right"><span>登录</span></div>
						</div>
					</li>-->
					<li class="aui-list-item address-root" v-for="(item,index) in getAddressListHome" :key="index" @click="getNow(item,index)" v-if="login">
						<div class="aui-media-list-item-inner">
							<div class="aui-list-item-label-icon">
								<i class="icon njfont nj-dizhi"></i>
							</div>
							<div class="aui-list-item-inner clear_b">
								<div class="aui-list-item-text">													<!--?item.receivername:userInfomation.mobile-->
									<div class="aui-list-item-title">
										<strong id="#uvipname" class="uvipname">收货人:{{item.receivername}}</strong>
										<span class="aui-font-size-14 text_c_gray"></span>
									</div>
								</div>
								<p class="address_float"><span class="aui-label aui-label-info">{{getLabel(item.label)}}</span>&nbsp;<span class="show-address">{{item.result || item.address}}</span><span class="show-address">&nbsp;&nbsp;{{item.houseNumber}}</span></p>
							</div>
							<div class="aui-list-item-label-icon" >
								<i class="icon njfont nj-xuanzhong" style="color: #74AE56;" v-if="item.isNew == 0 && flag"></i>
							</div>
						</div>
					</li>
				</ul>
			</div>
		</div>
</template>

<script>
	import { mapMutations, mapGetters } from 'vuex';
	import axios from 'axios';
	import {Toast} from '../../utils/toast';
	//	import { Popup } from 'mint-ui';
	export default {
		props: {
			
		},
		components: {
		},
		computed: {
            ...mapGetters('wxh',[
                "login"
            ]),
			...mapGetters('zx', [
				"getChooseAddressActiveIndex",
				"getAddressList",
			]),
			...mapGetters('qxz', [
				"getLocation","getStoreList"
			]),
			zxPageHeight(){
				return window.screen.height;
			},
			zxPageWidth(){
				return window.environment === "edit" ? 375 : window.screen.width;
			},
			getChooseAddressNow(){
				return this.chooseAddressNow;
			},
			getAddressListHome(){
				return this.myUserInfo ? this.myUserInfo.addressLists : [];
			},
		},
		data() {
			return {
				activeIndex:null,
				address:null, //地址列表
				chooseAddressNow:null,
				myUserInfo:null,
				flag:true,   // 搜索地址 替换原来的地址  选择置空
				clickFlag:false // 是否是点击选择地址
				
			}
		},
		mounted(){
		},
		created() {
			
			this.checkLogin();
			console.log(this.login,'*******************************是否登录')
		},
		beforeDestroy() {
		
	    },
		methods: {
			...mapMutations('zx',[
				"setHomeAddressNow",
				"setHomeStoreNow",
				"setIsShow",
				"setUseMapFlag",
				"setHomeAddressNow",
				"setChooseAddressActiveIndex",
			]),
			setAddress(){
				for(let i = 0; i < this.getAddressListHome.length; i++){
					if(this.getAddressListHome[i].isNew == 0){
						this.flag = true;
						localStorage.setItem('locateIn',JSON.stringify(this.getAddressListHome[i]));
						this.$store.dispatch('qxz/updateLocation', this.getAddressListHome[i]);
						this.chooseAddressNow = this.getAddressListHome[i];
//						this.$router.push('home');
					}
				}
			},
			getLabel(label){
				if(label == 0 || label == '家'){
					return '家';
				}else if(label == 1 || label == '公司'){
					return '公司';
				}else if(label == 2 || label == '其他'){
					return '其他';
				}
			},
			getNow(item,index){
				this.changeAddressIsNew(item);
			},
			//将当前点击的地址  isNew置为 0
			changeAddressIsNew(item){
				let params = {
					type:"UPDATE",
					userid: item.userid,
					addressid:item.addressid,
					enterprisecode:window.api.enterpriseCode
				}
				var sessionId = localStorage.getItem('sessionId');
				let config = {
					url: window.api.customer_host+'/address',
					method:'POST',
					data:params,
					headers: {'sessionId': sessionId, 'Content-Type':'application/json', 'Accept' : 'application/json'},
				}
				axios(config).then((res)=>{
					let time = new Date().getTime();
					let sessionId = localStorage.getItem('sessionId');
		            let url = window.api.customer_host+'/customer/getCustomer?token='+time;
					let config = {
						url: url,
						method:'get',
						headers: {'sessionId': sessionId, 'Content-Type':'application/json', 'Accept' : 'application/json'},
					}
					try {
						axios(config).then((res)=>{
							this.myUserInfo = res.data.data[0];
							this.setAddress();
						})
					}catch(err){
						console.log(err);
					}
				})
			},
			toLogin(){
				this.$router.push({path:'login',query:{page:"chooseAddress"}});
			},
			checkLogin(){
				if(this.login){
					this.getUserInfo();
				}else{
					console.log('未登錄');
				}
			},
			//获取地址信息
			getAddress(){
				if(this.myUserInfo && this.getStoreList){
					var addressList = this.myUserInfo.addressLists;
//					this.getStoreList
					var arr = this.getStoreList;
					var arrStore = [];
					for(let j = 0; j < arr.length; j++){
						arrStore.push(arr[j].storeCode);
					}
					var newArr = [];
					for(let i = 0; i < addressList.length; i++){
					     var index = arrStore.indexOf(addressList[i].storeCode);
//					     console.log(index,'是否存在');
					     if(index != -1){
					     	newArr.push(addressList[i]);
					     }
					}
					this.address = newArr;
				}else if(this.myUserInfo){
					this.address = this.myUserInfo.addressLists;
				}
				
				if(this.getLocation == null || this.getLocation.addressid == -1){
					this.flag = false;
				}else{
					if(this.address && this.address.length){
						for(let k = 0; k < this.address.length; k++ ){
							if(this.address[k].isNew == 0){
								this.$store.dispatch('qxz/updateLocation', this.address[k]);
								localStorage.setItem('locateIn',JSON.stringify(this.address[k]));
								this.flag = true;
							}
						}
					}
				}
			},
			cancelChoose(){
				this.activeIndex = null;
				this.chooseAddressNow = null;
			},
			getUserInfo(){
				let time = new Date().getTime();
				let sessionId = localStorage.getItem('sessionId');
	            let url = window.api.customer_host+'/customer/getCustomer?token='+time;
				let config = {
					url: url,
					method:'get',
					headers: {'sessionId': sessionId, 'Content-Type':'application/json', 'Accept' : 'application/json'},
				}
				try {
					console.log('qxz选择地址获取44444444444444444');
					axios(config).then((res)=>{
						this.myUserInfo = res.data.data[0];
						this.getAddress();
					})
				}catch(err){
					console.log(err);
				}
			}
		},
		watch: {
			getChooseAddressIndexComputed(newVal) {
				console.log("getChooseAddressIndexComputed--------", newVal);
			},
			getLocation(newVal,oldVal){
				console.log(newVal,oldVal,'getLocation七七')
				if(newVal == null || newVal.addressid == -1){
					this.flag = false;
				}
			},
			//解决 接口 返回调用顺序问题
			userInfo(newVal,oldVal){
				this.getUserInfo();
			}
		}
	}
</script>

<style scoped="scoped">
	.noLogin{
		width: 100%;
	    height: 2.2rem;
	    display: flex;
	    justify-content: space-between;
	    line-height: 2.2rem;
	    box-sizing: border-box;
	   	align-items: center;
	}
	.showT{
		padding-left: 0.75rem;
	}
	.showLogin{
		margin-right: 0.75rem;
		/*border: 1px solid #80B760;*/
		/*text-align: center;*/
		color: #80B760;
		/*vertical-align: middle;*/
	}
	.showLogin span{
		border: 1px solid #80B760;
		padding: 0.2rem 0.5rem;
		border-radius: 0.3rem;
	}
</style>